<template>
	<view class="container">
		<view v-if="prizeLogList.length">
			<view class="prize-item" v-for="item in prizeLogList" :key="item">
				<view class="content">
					<view class="left">
						<u-image width="140rpx" height="140rpx" :src="item.gift.cover_pic"></u-image>
					</view>
					<view class="right">
						<view class="prize-name">
							{{item.name}}
						</view>
						<view class="prize-num">
							奖品数量：{{item.num}}
						</view>
					</view>
				</view>
				<view class="footer">
					<view style="color: #999;">获得时间：{{item.create_time}}</view>
					<view :class="item.status == 0 ? 'btn red' : 'btn gray'" @click="handleBtnClick(item.status,item.id	)">
						{{item.status == 0 ? '立即兑换' : '删除记录'}}
					</view>
				</view>
			</view>
		</view>
		<view v-else class="empty-wrap">
			<u-empty text="奖品记录为空" mode="list"></u-empty>
		</view>
		
		<!-- 展示二维码的弹窗 -->
		<u-mask :show="showDialog" @click="showDialog = false">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="close-icon">
						<u-icon name="close" size="28" @click="closeIconClick"></u-icon>
					</view>
					<view class="content">
						<u-image width="100%" height="360rpx" :src="kfImg"></u-image>
						<view class="text">
							联系客服线下兑换
						</view>
						<u-button  shape="circle" type="error" @click="handleSaveImgClick">保存二维码</u-button>
					</view>
				</view>
			</view>
		</u-mask>
	</view>
</template>

<script>
	import {pondPrizeLog,delLog} from '@/api/pond.js'
	export default {
		data() {
			return {
				prizeLogList: [],
				showDialog: false,
				kfImg: ''
			}
		},
		created() {
			this.pondPrizeLogAPI()
		},
		methods: {
			handleBtnClick(type,id) {
				// type 0: 立即兑换 1：删除记录
				console.log(type)
				if (type == 0) {
					this.showDialog = true
				} else {
					uni.showModal({
						title: '提示',
						content: '是否删除记录？',
						success:(res) => {
							if (res.confirm) {
								this.delLogAPI({id})
							}
						}
					})
				}
			},
			closeIconClick() {
				console.log('icon')
				this.showDialog = false
			},
			handleSaveImgClick() {
				console.log('保存图片')
				uni.downloadFile({
					url: this.kfImg,
					success: (res) => {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: (res) => {
								if (res.errMsg) {
									uni.showToast({
									  title: "图片保存成功～"
									});
								}
								this.showDialog = false
							}
						})
					}
				})

			},
			async pondPrizeLogAPI() {
				const res = await pondPrizeLog()
				console.log(res)
				if (res.data) {
					this.kfImg = res.data.kf_wechat_img
					this.prizeLogList = res.data.list
				}
			},
			async delLogAPI(params) {
				const res = await delLog(params)
				if (res.code == 0) {
					uni.showToast({
						title: res.msg
					})
				}
				this.pondPrizeLogAPI()
			}
		}
	}
</script>

<style lang="less" scoped>
	.container {
		height: 100vh;
		background-color: #f2f2f2;
		padding: 20rpx 30rpx;
		.warp {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 100%;
			}
		
			.rect {
				width: 520rpx;
				height: 600rpx;
				border-radius: 20rpx;
				background-color: #fff;
				.close-icon {
					display: flex;
					justify-content: flex-end;
					margin-right: 20rpx;
					margin-top: 20rpx;
				}
				.content {
					.text {
						text-align: center;
						margin: 20rpx 0;
					}
					padding: 10rpx 56rpx 0;
				}
			}
		.prize-item {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			background-color: #fff;
			width: 100%;
			height: 270rpx;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-bottom: 30rpx;
			.content {
				display: flex;
				.left {
				}
				.right {
					margin-left: 20rpx;
					.prize-name {
						overflow: hidden;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						word-break: break-all;
					}
					.prize-num {
						font-size: 22rpx;
						color: #999;
						margin-top: 14rpx;
					}
				}
			}
			.footer {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.btn {
					height: 60rpx;
					width: 144rpx;
					border-radius: 120rpx 120rpx 120rpx 120rpx;
					text-align: center;
					line-height: 60rpx;
				}
				.gray {
					border: 1px solid #a1a1a1;
					color: #262626;
				}
				.red {
					border: 1px solid #F0250E;
					color: #F0250E;
				}
			}
		}
		
		.empty-wrap {
			height: 100vh;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
